// pages/activity/activity.js
const {
    getActivity,
    searchActivity,
} = require('../../services/activityReq');
const moment = require('../../utils/moment');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        pageNum: 1,
        searchPageNum: 1,
        pageSize: 10,
        activityList: [],
        searchList: [],
        // 监听是否加载完毕
        isFinish: false,
        isSearchFinish: false,
        searchValue: '',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        // 获取所有活动
        this.handleGetAllActivity();
    },

    onReachBottom: function () {
        if (this.data.searchValue === '') {
            this.setData({
                pageNum: this.data.pageNum + 1,
            });
            this.handleGetAllActivity();
        } else {
            this.setData({
                searchPageNum: this.data.searchPageNum + 1,
            });
            this.handleSearchActivity();
        }
    },


    // 获取全部活动
    handleGetAllActivity() {
        if (this.data.isFinish) {
            return;
        }
        wx.showLoading({
            title: '加载中',
        });

        getActivity(this.data.pageNum, this.data.pageSize, res => {
            let list = res.data.data.list;
            if (list.length < this.data.pageSize) {
                this.setData({
                    isFinish: true,
                });
            }
            list.forEach((v, i) => {
                v.activityDatetime = moment(v.activityDatetime).format('YYYY-MM-DD');
                try {
                    v.activityAddress = JSON.parse(v.activityAddress).address;
                } catch (e) {

                }
            });
            this.setData({
                activityList: this.data.activityList.concat(list),
            });
            wx.hideLoading();
        });
    },

    // 搜索更新
    handleSearchChange(e) {
        this.setData({
            searchValue: e.detail,
            searchList: [],
            searchPageNum: 1,
            isSearchFinish: false,
        });
    },

    // 搜索活动
    handleSearchActivity() {
        if (this.data.isSearchFinish) {
            return;
        }
        wx.showLoading({
            title: '搜索中',
            mask: true,
        });
        searchActivity(this.data.searchValue, this.data.searchPageNum, this.data.pageSize, res => {
            let list = res.data.data.list;
            if (list.length < this.data.pageSize) {
                this.setData({
                    isSearchFinish: true,
                });
            }
            list.forEach((v, i) => {
                v.activityDatetime = moment(v.activityDatetime).format('YYYY-MM-DD');
            });
            this.setData({
                searchList: this.data.searchList.concat(list),
            });
            wx.hideLoading();
        });
    },

    // 跳转活动详情
    handleGoDetail(e) {
        const id = e.currentTarget.dataset.id;
        wx.navigateTo({
            url: `../activityDetail/activityDetail?id=${id}`,
        });
    },
});
